<template>
    <div>
        <div style="display: flex;justify-content: space-between">
            <el-button icon="el-icon-plus" type="primary" round @click="showAddSalaryView">添加工资账套</el-button>
            <el-button icon="el-icon-refresh" type="success" @click="initSalaries"></el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table :data="salaries" border stripe align="center">
                <el-table-column type="selection" width="55" style="justify-content: space-between"></el-table-column>
                <el-table-column width="160" prop="name" label="账套名称" align="center"></el-table-column>
                <el-table-column width="80" prop="basicSalary" label="基本工资" align="center"></el-table-column>
                <el-table-column width="80" prop="trafficSalary" label="交通补助" align="center"></el-table-column>
                <el-table-column width="80" prop="lunchSalary" label="午餐补助" align="center"></el-table-column>
                <el-table-column width="80" prop="bonus" label="奖金" align="center"></el-table-column>
                <el-table-column width="120" prop="createDate" label="启用时间" align="center"></el-table-column>
                <el-table-column label="养老金" align="center">
                    <el-table-column width="90" prop="pensionPer" label="比率" align="center"></el-table-column>
                    <el-table-column width="90" prop="pensionBase" label="基数" align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="医疗保险" align="center">
                    <el-table-column width="90" prop="medicalPer" label="比率" align="center"></el-table-column>
                    <el-table-column width="90" prop="medicalBase" label="基数" align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="公积金" align="center">
                    <el-table-column width="90" prop="accumulationFundPer" label="比率" align="center"></el-table-column>
                    <el-table-column width="90" prop="accumulationFundBase" label="基数" align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" align="center" round @click="showEditSalaryView(scope.row)">编辑
                        </el-button>
                        <el-button type="danger" align="center" round @click="deleteSalaryById(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                :title="dialogTitle"
                :visible.sync="dialogVisible"
                width="50%">
            <div style="display: flex;justify-content: space-around;align-items: center">
                <el-steps direction="vertical" :active="activeItemIndex">
                    <el-step :title="itemName" v-for="(itemName,index) in salaryItemName" :key="index"></el-step>
                </el-steps>
                <el-input v-model="salary[key]" :placeholder="'请输入'+salary[activeItemIndex]+'...'"
                          v-for="(value,key,index) in salary" :key="index" v-show="activeItemIndex===index"
                          style="width: 200px"/>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="preStep">{{activeItemIndex===10?'取消':'上一步'}}</el-button>
                <el-button type="primary" @click="nextStep">{{activeItemIndex==10?'完成':'下一步'}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "SalSob",
        data() {
            return {
                salaries: [],
                dialogVisible: false,  //添加框展示控制变量
                dialogTitle: '添加工资账套',
                activeItemIndex: 0, //首先展示的哪一个变量名称
                salaryItemName: [  //变量名称
                    '账套名称',
                    '基本工资',
                    '交通补助',
                    '午餐补助',
                    '奖金',
                    '养老金比率',
                    '养老金基数',
                    '医疗保险比率',
                    '医疗保险基数',
                    '公积金比率',
                    '公积金基数',

                ],
                salary: {   //初始化输入框中的数字内容
                    name: '',
                    basicSalary: 0,
                    trafficSalary: 0,
                    lunchSalary: 0,
                    bonus: 0,
                    pensionPer: 0,
                    pensionBase: 0,
                    medicalPer: 0,
                    medicalBase: 0,
                    accumulationFundPer: 0,
                    accumulationFundBase: 0,

                }
            }
        },
        mounted() {
            this.initSalaries();
        },
        methods: {
            preStep() {
                if (this.activeItemIndex === 0) {
                    return;
                } else if (this.activeItemIndex === 10) {
                    this.dialogVisible = false;
                    return;
                }
                this.activeItemIndex--;
            },
            nextStep() {
                if (this.activeItemIndex === 10) {
                    if (this.salary.id) {
                        this.putRequest("/salary/sob/", this.salary).then(resp => {
                            if (resp) {
                                this.initSalaries();
                                this.dialogVisible = false;
                            }
                        })
                    } else {
                        this.postRequest("/salary/sob/", this.salary).then(resp => {
                            if (resp) {
                                this.initSalaries();
                                this.dialogVisible = false;
                            }
                        });
                    }
                    return;
                }
                this.activeItemIndex++;
            },
            //展示添加框
            showAddSalaryView() {
                this.salary = {   //初始化输入框中的数字内容
                    name: '',
                    basicSalary: 0,
                    trafficSalary: 0,
                    lunchSalary: 0,
                    bonus: 0,
                    pensionPer: 0,
                    pensionBase: 0,
                    medicalPer: 0,
                    medicalBase: 0,
                    accumulationFundPer: 0,
                    accumulationFundBase: 0,

                }
                this.dialogTitle = "添加工资账套"
                this.activeItemIndex = 0;
                this.dialogVisible = true;
            },
            showEditSalaryView(data) {
                this.dialogTitle = '修改工资账套';
                this.dialogVisible = true;
                this.salary.basicSalary = data.basicSalary;
                this.salary.trafficSalary = data.trafficSalary;
                this.salary.lunchSalary = data.lunchSalary;
                this.salary.bonus = data.bonus;
                this.salary.pensionPer = data.pensionPer;
                this.salary.pensionBase = data.pensionBase;
                this.salary.medicalPer = data.medicalPer;
                this.salary.medicalBase = data.medicalBase;
                this.salary.accumulationFundPer = data.accumulationFundPer;
                this.salary.accumulationFundBase = data.accumulationFundBase;
                this.salary.name = data.name;
                this.salary.id = data.id;
            },
            initSalaries() {
                this.getRequest("/salary/sob/").then(resp => {
                    this.salaries = resp;
                })
            },
            deleteSalaryById(data) {
                this.$confirm('此操作将删除【' + data.name + '】账套，是否继续？', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定'
                }).then(() => {
                    this.deleteRequest("/salary/sob/" + data.id).then(resp => {
                        if (resp) {
                            this.initSalaries();
                        }
                    })
                }).catch(() => {
                    this.$message.info("取消删除!");
                })
            },
        }
    }
</script>

<style scoped>

</style>
